package com.selly.hanjuang.lib;

import java.util.ArrayList;
import java.util.HashMap;

import com.selly.hanjuang.Bandrek;
import com.selly.hanjuang.MenuCart;
import com.selly.hanjuang.MenuTransaksi;
import com.selly.hanjuang.R;
import com.selly.hanjuang.R.id;
import com.selly.hanjuang.R.layout;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ListAdapterCart extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	// private String sid_produk;
	public static final String TAG_DELETE = "indeks_delete";
	public static final String TAG_UPDATE = "indeks_update";

	boolean clicked = false;
	//ArrayList<Integer> qtyBandrekList = new ArrayList<Integer>();

	// int posTemp = 0;
	// int qtyTemp = 1;

	public ListAdapterCart(Activity a, ArrayList<HashMap<String, String>> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, final View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = convertView; 
		final View vi2 = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.item_list_cart, null);

		ImageView thumb_image = (ImageView) vi
				.findViewById(R.id.image_produk_cart);

		TextView id_produk = (TextView) vi.findViewById(R.id.id_produk);
		TextView link_image_produk = (TextView) vi
				.findViewById(R.id.link_image_produk);
		TextView nama_produk = (TextView) vi.findViewById(R.id.nama_produk);
		TextView harga = (TextView) vi.findViewById(R.id.harga);

		// TextView qty = (TextView) vi.findViewById(R.id.txtQty);

		//Spinner spinnerQty = (Spinner) vi.findViewById(R.id.spinner);
		//spinnerQty.setOnItemSelectedListener(new CustomOnItemSelectedListener( position));

		TextView totalHarga = (TextView) vi.findViewById(R.id.totalHarga);

		HashMap<String, String> daftar_produk = new HashMap<String, String>();
		daftar_produk = data.get(position);

		id_produk.setText(daftar_produk.get("id_produk"));
		link_image_produk.setText(daftar_produk.get("link_image"));

		Log.d("cikan link_image", daftar_produk.get("link_image"));

		nama_produk.setText(daftar_produk.get("nama_produk"));
		harga.setText("Rp " + daftar_produk.get("harga"));
		imageLoader.DisplayImage(daftar_produk.get("link_image"), thumb_image);
		// spinnerQty.setSelection(0);
		//int idx = getIndeks(spinnerQty, daftar_produk.get("qty"));
		// posTemp = idx;
		//qtyBandrekList.add(idx);
		//spinnerQty.setSelection(idx);

		totalHarga.setText("Total : Rp " + daftar_produk.get("totalHarga"));

		ImageButton btnDelete = (ImageButton) vi.findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// onListItemClick(this, this, position, null);
				data.remove(position);
				notifyDataSetChanged();

				Intent in = new Intent(activity.getApplicationContext(),
						MenuCart.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				in.putExtra(TAG_DELETE, String.valueOf(position));
				activity.getApplicationContext().startActivity(in);
			}
		});

		Button btnOk = (Button) vi.findViewById(R.id.btnOKQty);

		final EditText etQty = (EditText) vi.findViewById(R.id.txtQty); 
		etQty.setText(daftar_produk.get("qty"));
		
		btnOk.setOnClickListener( new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				
				String s = etQty.getText().toString();
				int qty = Integer.parseInt(etQty.getText().toString());
				//Log.d("et : ", "et is " + s);
				
				Intent in = new Intent(activity.getApplicationContext(), MenuCart.class)
						.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				in.putExtra(TAG_UPDATE, String.valueOf(position));
				activity.getApplicationContext().startActivity(in);

				ArrayList<Bandrek> bandrekList = ((MyApplication) activity
						.getApplication()).getBandrekList();

				Bandrek bandrek = bandrekList.get(position);

				// int qty =
				// Integer.parseInt(parent.getItemAtPosition(pos).toString());
				bandrek.setQty(qty);
				bandrek.setTotalHarga(qty * bandrek.getHarga());

				bandrekList.set(position, bandrek);
			}
		});
			
			
		return vi;
	}

	/*
	private int getIndeks(Spinner spinner, String myString) {

		int index = 0;

		for (int i = 0; i < spinner.getCount(); i++) {
			if (spinner.getItemAtPosition(i).equals(myString)) {
				index = i;
			}
		}
		return index;
	}
	*/

	protected void onListItemClick(OnClickListener onClickListener,
			OnClickListener onClickListener2, int position, Object object) {
		Log.i("test button", "cooooy button :D -> " + position);
	}

	/*
	private void updateQty(View v, int position) {
		EditText et = (EditText) v.findViewById(R.id.txtQty);
		
		Toast.makeText(activity, "yeah : " + et.getText().toString(), Toast.LENGTH_SHORT).show();
		//Log.d("testcik ET QTY : ", et.getText().toString());

			}

	
	public class CustomOnItemSelectedListener implements OnItemSelectedListener {

		int positionBandrek;

		public CustomOnItemSelectedListener(int position) {
			// TODO Auto-generated constructor stub
			this.positionBandrek = position;
		}

		@Override
		public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
				long arg3) {
			// TODO Auto-generated method stub
			
			// if(posTemp!=pos){
			if (qtyBandrekList.get(positionBandrek) != pos) {
				Intent in = new Intent(activity.getApplicationContext(),
						MenuCart.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				in.putExtra(TAG_UPDATE, String.valueOf(positionBandrek));
				activity.getApplicationContext().startActivity(in);

				ArrayList<Bandrek> bandrekList = ((MyApplication) activity
						.getApplication()).getBandrekList();

				Bandrek bandrek = bandrekList.get(positionBandrek);

				int qty = Integer.parseInt(parent.getItemAtPosition(pos)
						.toString());
				bandrek.setQty(qty);
				bandrek.setTotalHarga(qty * bandrek.getHarga());

				bandrekList.set(positionBandrek, bandrek);

				// posTemp = pos;
				qtyBandrekList.set(positionBandrek, pos);
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
		}

	}

	public class CustomOnClickListener2 implements OnClickListener {

		int positionBandrek;

		public CustomOnClickListener2(int position) {
			// TODO Auto-generated constructor stub
			this.positionBandrek = position;
		}

		// @Override
		public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
				long arg3) {
			// TODO Auto-generated method stub
			
			// if(posTemp!=pos){
			if (qtyBandrekList.get(positionBandrek) != pos) {
				Intent in = new Intent(activity.getApplicationContext(),
						MenuCart.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				in.putExtra(TAG_UPDATE, String.valueOf(positionBandrek));
				activity.getApplicationContext().startActivity(in);

				ArrayList<Bandrek> bandrekList = ((MyApplication) activity
						.getApplication()).getBandrekList();

				Bandrek bandrek = bandrekList.get(positionBandrek);

				int qty = Integer.parseInt(parent.getItemAtPosition(pos)
						.toString());
				bandrek.setQty(qty);
				bandrek.setTotalHarga(qty * bandrek.getHarga());

				bandrekList.set(positionBandrek, bandrek);

				// posTemp = pos;
				qtyBandrekList.set(positionBandrek, pos);
			}

		}

		@Override
		public void onClick(View vi) {
			// TODO Auto-generated method stub
			// if(qtyBandrekList.get(positionBandrek)!=pos){

			EditText et = (EditText) vi.findViewById(R.id.txtQty);
			int qty = Integer.parseInt(et.getText().toString());

			Intent in = new Intent(activity.getApplicationContext(),
					MenuCart.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			in.putExtra(TAG_UPDATE, String.valueOf(positionBandrek));
			activity.getApplicationContext().startActivity(in);

			ArrayList<Bandrek> bandrekList = ((MyApplication) activity
					.getApplication()).getBandrekList();

			Bandrek bandrek = bandrekList.get(positionBandrek);

			// int qty =
			// Integer.parseInt(parent.getItemAtPosition(pos).toString());
			bandrek.setQty(qty);
			bandrek.setTotalHarga(qty * bandrek.getHarga());

			bandrekList.set(positionBandrek, bandrek);

			// posTemp = pos;
			// qtyBandrekList.set(positionBandrek, pos);
			// }
		}

	}
	*/

}
